package com.app.service.Impl;

import com.app.common.CalculateDistance;
import com.app.mapper.EntrepreneurshipMapper;
import com.app.model.CategoryModel;
import com.app.model.EntrepreneurshipModel;
import com.app.model.UserModel;
import com.app.service.CategoryService;
import com.app.service.EntrepreneurshipService;
import com.app.service.UserService;
import com.app.vo.EntrepreneurshipVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

@Service
public class EntrepreneurshipServiceImpl implements EntrepreneurshipService {
    @Autowired
    private EntrepreneurshipMapper entrepreneurshipMapper;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private UserService userService;

    @Override
    public void update(EntrepreneurshipModel model) {
        entrepreneurshipMapper.updateById(model);
    }

    @Override
    public void insert(EntrepreneurshipModel model) {
        entrepreneurshipMapper.insert(model);
    }

    @Override
    public void deleteById(Integer id) {
        entrepreneurshipMapper.deleteById(id);
    }

    @Override
    public EntrepreneurshipVo selectList(Integer page, Integer limit, String keyword, Integer type, Integer status, Integer township) {
        EntrepreneurshipVo vo = new EntrepreneurshipVo();
        page = (page - 1) * limit;
        List<EntrepreneurshipModel> enList = entrepreneurshipMapper.getEnList(page, limit, keyword, type, status,township);
        for (int i = 0; i < enList.size(); i++) {
            CategoryModel categoryById = categoryService.getCategoryById(enList.get(i).getType());
            enList.get(i).setTypeText(categoryById.getName());
            String[] split = enList.get(i).getSwiperImage().split(",");
            enList.get(i).setSwiperImages(Arrays.asList(split));
            if (enList.get(i).getStatus() == 0) {
                enList.get(i).setStatusText("待审核");
            }
            if (enList.get(i).getStatus() == 1) {
                enList.get(i).setStatusText("通过");
            }
            if (enList.get(i).getStatus() == 2) {
                enList.get(i).setStatusText("拒绝");
            }
            if (enList.get(i).getUserId() != null) {
                UserModel model = userService.selectUserById(enList.get(i).getUserId());
                enList.get(i).setUserModel(model);
            }
        }
        int count = entrepreneurshipMapper.getEnCount(keyword, type, status);
        vo.setList(enList);
        vo.setCount(count);
        return vo;
    }

    @Override
    public EntrepreneurshipVo selectList(Integer page, Integer limit, String keyword, Integer type, Double longitude, Double latitude, Integer township) {
        EntrepreneurshipVo vo = new EntrepreneurshipVo();
        page = (page - 1) * limit;
        List<EntrepreneurshipModel> enList = entrepreneurshipMapper.selectEnList(page, limit, keyword, type,township);
        for (int i = 0; i < enList.size(); i++) {
            CategoryModel categoryById = categoryService.getCategoryById(enList.get(i).getType());
            enList.get(i).setTypeText(categoryById.getName());
            if (enList.get(i).getSwiperImage() != null && !enList.get(i).getSwiperImage().equals("")) {
                String[] split = enList.get(i).getSwiperImage().split(",");
                enList.get(i).setSwiperImages(Arrays.asList(split));
            }
            if (enList.get(i).getStatus() == 0) {
                enList.get(i).setStatusText("待审核");
            }
            if (enList.get(i).getStatus() == 1) {
                enList.get(i).setStatusText("通过");
            }
            if (enList.get(i).getStatus() == 2) {
                enList.get(i).setStatusText("拒绝");
            }
            if (enList.get(i).getUserId() != null) {
                UserModel model = userService.selectUserById(enList.get(i).getUserId());
                enList.get(i).setUserModel(model);
            }
            if (enList.get(i).getLongitude() != null && enList.get(i).getLatitude() != null) {
                double longitude1 = Double.parseDouble(enList.get(i).getLongitude());
                double latitude1 = Double.parseDouble(enList.get(i).getLatitude());
                if (latitude != null && longitude != null) {
                    Double haversine = CalculateDistance.haversine(latitude1, longitude1, latitude, longitude);
                    if (haversine != 0) {
                        DecimalFormat df = new DecimalFormat("0.0");
                        String formatted = df.format(haversine);
                        enList.get(i).setDistance(formatted + "km");
                    }
                } else {
                    enList.get(i).setDistance("暂无");
                }
            } else {
                enList.get(i).setDistance("暂无");
            }
        }
        int count = entrepreneurshipMapper.selectEnCount(keyword, type);
        vo.setList(enList);
        vo.setCount(count);
        return vo;
    }

    @Override
    public EntrepreneurshipModel selectById(Integer id) {
        EntrepreneurshipModel model = entrepreneurshipMapper.selectById(id);
        if (model.getType() != null) {
            CategoryModel categoryById = categoryService.getCategoryById(model.getType());
            if (categoryById != null) {
                model.setTypeText(categoryById.getName());
            }
        }
        if (model.getSwiperImage() != null && !model.getSwiperImage().equals("")) {
            String[] split = model.getSwiperImage().split(",");
            model.setSwiperImages(Arrays.asList(split));
        }
        if (model.getLabel() != null && !model.getLabel().equals("")) {
            String[] split = model.getLabel().split(",");
            model.setLabels(Arrays.asList(split));
        }
        return model;
    }

    @Override
    public List<EntrepreneurshipModel> selectByUserId(Integer id) {
        QueryWrapper<EntrepreneurshipModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        return entrepreneurshipMapper.selectList(queryWrapper);
    }

    /**
     * 查找由关键词
     *
     * @param queryWrapper 查询包装器
     * @return {@link List}<{@link EntrepreneurshipModel}>
     */
    @Override
    public List<EntrepreneurshipModel> selectByKeywords(QueryWrapper<EntrepreneurshipModel> queryWrapper) {

        return this.entrepreneurshipMapper.selectList(queryWrapper);
    }
}
